﻿package com.dLibs.photos
{
	import flash.display.MovieClip;
	import flash.events.MouseEvent;
	import flash.display.Loader;
	import flash.net.URLRequest;
	import flash.events.*;
	import flash.text.*;
	
	import caurina.transitions.Tweener;
	import com.dLibs.utils.DrawRectangle;
	
	public class picGall
	{
		public static var _prevClip		:MovieClip;		// create MovieClip prev Clip
		public static var _nextClip		:MovieClip;		// create MovieClip next Clip
		public static var _maskClip		:MovieClip;		// create to mask photos
		public static var _picClip		:MovieClip;		// create MovieClip that load pics
		public static var _closeClip	:MovieClip;
		public static var _nextBt		:MovieClip;
		public static var _prevBt		:MovieClip;
		public static var _preload		:MovieClip;
		
		public static var picBigImage	:MovieClip;
		public static var pic			:MovieClip;			// clip to uniq thumb Image
		public static var picBig		:MovieClip;			// clip to uniq Big Image
		public static var cmcRight		:MovieClip;
		public static var cmcLeft		:MovieClip;
		
		public static var _time			:Number;				// time of animation
		public static var iNum			:Number;				// to use into .for.
		public static var atualPicX		:Number;			// position of _picClip in .X.
		public static var atualPage		:Number;
		public static var atualID		:Number;
		public static var totalImg		:Number;
		public static var perc			:Number;
		
		public static var picLoader		:Loader;			// new Loader for thumbs
		public static var picBigLoader	:Loader;		// new Loader for big images
				
		public static var smallImage	:Array; 		// array that ll store thumbs
		public static var bigImage		:Array;			// array that store the loader names
		public static var _spData		:Array;
		
		public static var rtext			:TextField;
		public static var ltext			:TextField;
		public static var myFormat		:TextFormat; 
		
		public static var _enable		:Boolean = true;
		public static var initGall		:Boolean = false;
		
		public function picGall()
		{
			trace("another public funcition");
		};
		
		public static function addItem(spData:Array, bpData:Array, picArgs:Object):void
		{
			picLoader 		= 	new Loader();
			
			bigImage		=	new Array();
			smallImage		=	new Array();
			_spData			= 	new Array();
			_spData 		= 	spData;
			
			_prevClip 		= 	MovieClip(picArgs.prevClip);
			_nextClip 		= 	MovieClip(picArgs.nextClip);
			_maskClip 		=	MovieClip(picArgs.maskClip);
			_picClip		=	MovieClip(picArgs.picClip);
			_closeClip		=	MovieClip(picArgs.closeBt);
			_nextBt			=	MovieClip(picArgs.nextBt);
			_prevBt			=	MovieClip(picArgs.prevBt);
			_preload		=	MovieClip(picArgs.preload);
			
			_time			= 	Number(picArgs.timer);
			atualPage		=	Number(0);
			
			 myFormat 		= new TextFormat();
			 myFormat.color	= 0xFFFFFF; 
			 myFormat.size 	= 14; 
			 myFormat.font 	= "Verdana";
			 myFormat.bold 	= true;
			
			_picClip.x			= _maskClip.x;
			_picClip.mask 		= _maskClip;
			_closeClip.visible 	= false;
			
			for ( var pgint:uint = 0; pgint < _picClip.numChildren; pgint++ ) 
			{
				_picClip.removeChildAt(0);
			}
			
			totalImg		= Number(spData.length);
			
			for (iNum = 0; iNum < spData.length; iNum++)
			{
				picLoader = new Loader();
				picLoader.load(new URLRequest(spData[iNum]));
			
				pic = new MovieClip();
				pic.x = iNum * 158;
				pic.addChild(picLoader);
				pic.buttonMode = true;
				pic.name = "pic" + iNum;
				pic.id = iNum;
				pic.addEventListener(MouseEvent.CLICK, showBigPic);
				
				smallImage.push(_spData[iNum]);
				bigImage.push(bpData[iNum]);
				
				_picClip.addChild(pic);
			};
			
			// _prevClip.buttonMode = _nextClip.buttonMode = true;
			_prevClip.addEventListener(MouseEvent.CLICK, prevPictures);
			_nextClip.addEventListener(MouseEvent.CLICK, nextPictures);
		};
		
		public static function nextPictures(event:MouseEvent):void
		{
			if (!_enable) return;
			_enable = false;
			
			atualPage++;
			atualPicX = _picClip.x;
			if ( atualPage <= (Math.round(_spData.length / 3)) ) 
			{ 
				Tweener.addTween(_picClip, { x : atualPicX - (474), time : .8, onComplete : function():void { _enable = true; } });
				// trace(atualPage , (Math.round(_spData.length / 3)))
			} 
			else 
			{
				atualPage = (Math.round(_spData.length / 3))
				_enable = true;
			};
		};
		
		public static function prevPictures(event:MouseEvent):void
		{
			if (!_enable) return;
			_enable = false;
			
			atualPage--;
			atualPicX = _picClip.x;
			if ( atualPage >= 0 ) 
			{ 
				Tweener.addTween(_picClip, { x : atualPicX + (474), time : .8, onComplete : function():void { _enable = true; } });
				// trace(atualPage , (Math.round(_spData.length / 3)))
			} 
			else 
			{
				atualPage = 0;
				_enable = true;
			};
		};
		
		public static function showBigPic(event:MouseEvent):void
		{
			atualID 	= event.currentTarget.id;
			
			picBigLoader = new Loader();
			picBigLoader.load(new URLRequest(bigImage[atualID]));
			picBigLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, bigPicLoading);
			picBigLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, bigPicLoaded);
			
			picBig = new MovieClip();
			picBig.alpha = 0;
			
			event.currentTarget.root.stage.addChild(picBig);
			event.currentTarget.root.stage.addChild(_nextBt);
			event.currentTarget.root.stage.addChild(_prevBt);
			event.currentTarget.root.stage.addChild(_closeClip);
			event.currentTarget.root.stage.addChild(_preload);
			
			var shape:DrawRectangle = new DrawRectangle(0,0,picBig.root.stage.stageWidth,picBig.root.stage.stageHeight,0x000000,0.5);
			picBig.addChild(shape);
			
			picBigImage = new MovieClip();
			picBigImage.addChild(picBigLoader);
			
			picBig.addChild(picBigImage);
		};
		
		public static function bigPicLoading(event:ProgressEvent):void 
		{
			_preload.visible = true;
			_preload.x = picBig.root.stage.stageWidth/2;
			_preload.y = picBig.root.stage.stageHeight/2;
			picBig.root.stage.setChildIndex(_preload,picBig.root.stage.numChildren - 1);
		}
		
		public static function bigPicLoaded(event:Event):void
		{
			_preload.visible = false;
			picBigImage.x = (picBig.root.stage.stageWidth - picBigImage.width) / 2;
			picBigImage.y = (picBig.root.stage.stageHeight - picBigImage.height) / 2;
			Tweener.addTween(picBig, { alpha : 1, time : 2 });
			Tweener.addTween(picBigImage, { alpha : 1, time : 2 });
			
			_closeClip.visible 		= true;
			Tweener.addTween(_closeClip, {
				alpha : 1,
				x : picBigImage.x + picBigImage.width - (_closeClip.width/2),
				y : picBigImage.y - (_closeClip.height/2),
				time : 1 });
			_closeClip.buttonMode 	= true;
			_closeClip.addEventListener(MouseEvent.CLICK, bigPicClose);
			
			_nextBt.visible			= true;
			Tweener.addTween(_nextBt, {
				alpha : 1,
				x : picBigImage.x + picBigImage.width + ( _nextBt.width + 10 ),
				y : picBig.root.stage.stageHeight/2,
				time : 1 });
			_nextBt.buttonMode		= true;
			_nextBt.addEventListener(MouseEvent.CLICK, loadNextImage);
			if ( ( atualID + 1 ) == totalImg ) _nextBt.visible = false;
			
			_prevBt.visible			= true;
			Tweener.addTween(_prevBt, {
				alpha : 1,
				x : picBigImage.x - ( _prevBt.width + 10 ),
				y : picBig.root.stage.stageHeight/2,
				time : 1 });
			_prevBt.buttonMode		= true;
			_prevBt.addEventListener(MouseEvent.CLICK, loadPrevImage);
			if ( atualID == 0 ) _prevBt.visible = false;
			
			picBig.root.stage.setChildIndex(_prevBt,picBig.root.stage.numChildren - 1);
			picBig.root.stage.setChildIndex(_nextBt,picBig.root.stage.numChildren - 1);
			picBig.root.stage.setChildIndex(_closeClip,picBig.root.stage.numChildren - 1);
			picBig.root.stage.setChildIndex(_preload,picBig.root.stage.numChildren - 1);
		};
		
		public static function loadNextImage(e:MouseEvent):void {
			if ( picBig.contains(picBigImage) ) picBig.removeChild(picBigImage);
			atualID++;
						
			picBigLoader = new Loader();
			picBigImage.alpha = 0;
			picBigLoader.load(new URLRequest(bigImage[atualID]));
			picBigLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, bigPicLoading);
			picBigLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, bigPicLoaded);
			
			picBigImage = new MovieClip();
			picBigImage.addChild(picBigLoader);
			picBig.addChild(picBigImage);
		}
		
		public static function loadPrevImage(e:MouseEvent):void {
			if ( picBig.contains(picBigImage) ) picBig.removeChild(picBigImage);
			atualID--;
						
			picBigLoader = new Loader();
			picBigImage.alpha = 0;
			picBigLoader.load(new URLRequest(bigImage[atualID]));
			picBigLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, bigPicLoading);
			picBigLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, bigPicLoaded);
			
			picBigImage = new MovieClip();
			picBigImage.addChild(picBigLoader);
			picBig.addChild(picBigImage);
		}
		
		public static function escapeGall(event:KeyboardEvent):void
		{
			trace(event.keyCode);
		}
		
		public static function bigPicClose(event:MouseEvent):void
		{
			Tweener.addTween(picBig, { alpha : .5, time : .3, onComplete: function():void { event.currentTarget.root.stage.removeChild(picBig); initGall = false; } });
			_closeClip.visible 	= false;
			_nextBt.visible 	= false;
			_prevBt.visible 	= false;
			_preload.visible	= false;
		};
		
	};
};